package com.uzai.console.service.groupcarddata.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.dto.groupcarddata.AddFanGroupDto;
import com.uzai.console.dto.groupcarddata.BatchSetAddFanGroupDto;
import com.uzai.console.dto.groupcarddata.FindAddFanGroupDto;
import com.uzai.console.dto.groupcarddata.SetAddFanGroupDto;
import com.uzai.console.jsonvo.groupcarddata.GroupInfo;
import com.uzai.console.service.groupcarddata.GroupCardDataService;
import com.uzai.console.vo.groupcarddata.AddFanGroupVo;
import com.uzai.console.vo.groupcarddata.FindAddFanGroupVo;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.pqc.crypto.newhope.NHOtherInfoGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: liuqi
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class GroupCardDataServiceImpl implements GroupCardDataService {

    private static Logger logger = LoggerFactory.getLogger(GroupCardDataServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询当前进粉群
     * @author liuqi
     * @date 2022/11/23 11:38
     * @param
     * @return pyqReportDto
     */
    public FindAddFanGroupVo findAddFanGroup(FindAddFanGroupDto findAddFanGroupDto){

        FindAddFanGroupVo findAddFanGroupVo = new FindAddFanGroupVo();
        List<AddFanGroupVo> addFanGroupVoList = new ArrayList<>();

        List<AddFanGroupDto> addFanGroupDtoList = findAddFanGroupDto.getAddFanGroupDtoList();
        if(addFanGroupDtoList == null || addFanGroupDtoList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        for (AddFanGroupDto addFanGroupDto : addFanGroupDtoList) {
            if(addFanGroupDto.getDeviceUniqueId() == null || StringUtils.isBlank(addFanGroupDto.getKeyIndex())){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "群卡片唯一key和机器人都不能为空");
            }

            AddFanGroupVo addFanGroupVo = new AddFanGroupVo();
            addFanGroupVo.setDeviceUniqueId(addFanGroupDto.getDeviceUniqueId());
            addFanGroupVo.setKeyIndex(addFanGroupDto.getKeyIndex());

            //获取进粉群下标
            String groupIndex = String.format(RedisCacheKeyEnum.ORDER_PULL_GROUP_CACHE.getKey(), addFanGroupDto.getKeyIndex(), addFanGroupDto.getDeviceUniqueId());
            Object object = redisTemplate.opsForValue().get(groupIndex);
            if(object == null){
                addFanGroupVo.setGroupIndex(0);
            }else{
                addFanGroupVo.setGroupIndex(Integer.parseInt(object.toString()));
            }
            addFanGroupVoList.add(addFanGroupVo);
        }

        findAddFanGroupVo.setAddFanGroupList(addFanGroupVoList);

        return findAddFanGroupVo;
    }

    /**
     * 设置当前进粉群
     * @author liuqi
     * @date 2022/11/23 11:38
     * @param
     * @return pyqReportDto
     */
    public void setAddFanGroup(SetAddFanGroupDto setAddFanGroupDto){
        if(setAddFanGroupDto.getDeviceUniqueId() == null || StringUtils.isBlank(setAddFanGroupDto.getKeyIndex()) | setAddFanGroupDto.getGroupIndex() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "群卡片唯一key和机器人以及进粉群下标都不能为空");
        }
        String groupIndex = String.format(RedisCacheKeyEnum.ORDER_PULL_GROUP_CACHE.getKey(), setAddFanGroupDto.getKeyIndex(), setAddFanGroupDto.getDeviceUniqueId());

        redisTemplate.opsForValue().set(groupIndex, setAddFanGroupDto.getGroupIndex());
    }

    /**
     * 批量设置当前进粉群
     * @author liuqi
     * @date 2022/11/23 11:38
     * @param
     * @return pyqReportDto
     */
    public void batchSetAddFanGroup(BatchSetAddFanGroupDto batchSetAddFanGroupDto){
        List<SetAddFanGroupDto> setAddFanGroupDtoList = batchSetAddFanGroupDto.getSetAddFanGroupDtoList();
        if(setAddFanGroupDtoList == null || setAddFanGroupDtoList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入批量设置列表");
        }

        for (SetAddFanGroupDto setAddFanGroupDto : setAddFanGroupDtoList) {
            setAddFanGroup(setAddFanGroupDto);
        }
    }

    /**
     * 处理群卡片数据格式
     * @author liuqi
     * @date 2022/11/23 11:38
     * @param
     * @return pyqReportDto
     */
    public Map<GroupInfo, List<GroupInfo>> setAddFanGroup(@RequestBody List<GroupInfo> group_info){
        Map <GroupInfo, List<GroupInfo>> group_info_new = new HashMap<>();
        if(group_info == null || group_info.size() == 0){
            return group_info_new;
        }

        //生成保存机器人的群信息
        Map<Long, List<GroupInfo>> collect = group_info.stream().collect(Collectors.groupingBy(GroupInfo::getDevice_unique_id));
        if(collect == null || collect.size() == 0){
            return group_info_new;
        }

        for (Map.Entry<Long, List<GroupInfo>> longListEntry : collect.entrySet()) {
            List<GroupInfo> value = longListEntry.getValue();
            GroupInfo groupInfo_0 =  value.get(0);
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setDevice_id(groupInfo_0.getDevice_id());
            groupInfo.setDevice_name(groupInfo.getDevice_name());
            groupInfo.setDevice_unique_id(groupInfo.getDevice_unique_id());
            groupInfo.setDevice_remark(groupInfo.getDevice_remark());
            group_info_new.put(groupInfo, value);
        }

        return group_info_new;
    }


}
